<?php
/*
 * @开发工具: JetBrains PhpStorm.
 * @文件名：loan.class.php
 * @类功能: 贷款产品
 * @开发者: 陈旭林
 * @开发时间： 2017-09-01
 * @版本：version 1.0
 */

namespace app\partner\controller;


use app\common\cache\agent\IAgentAdminCache;
use app\common\cache\agent\IAgentCache;
use app\common\controller\PartnerBaseController;
use app\common\enum\IsAnonymous;
use app\common\enum\IsPhone;
use app\common\ext\IDb;

class AgentAdmin extends PartnerBaseController {

    /**
     * @功能：取得推荐顾问详情
     * @开发者：gys
     */
    public function agent_admin_detail(){
        //接收参数
        $agent_admin_id  = self::get_data('agent_admin_id',self::NOT_EMPTY,"顾问编号不能为空！");

        $agent_admin_info = IAgentAdminCache::getInfo($agent_admin_id,['agent_id','real','avatar','phone','evaluate_rate','business_phone','service_ote','is_company','is_real']);
        if(empty($agent_admin_info)){
            self::set_code(self::WARNING_0001);
            self::set_msg("取得顾问详情失败！");
            self::send();
        }

        if(!empty($agent_admin_info['phone'])){
            $agent_admin_info['is_phone'] = IsPhone::yes;
        }else{
            $agent_admin_info['is_phone'] = IsPhone::not;
        }

        if(empty($agent_admin_info['business_phone'])){
            $agent_admin_info['business_phone'] = $agent_admin_info['phone'];
        }
        unset($agent_admin_info['phone']);

        $agent_info = IAgentCache::getInfo($agent_admin_info['agent_id'],['agent_name','business_license_url','is_bond','bond','business_phone']);

        $is_exist = IDb::getInstance('agent_admin_favorite')
            ->setDbWhere(['user_id'=>self::$uid,'agent_admin_id'=>$agent_admin_id])
            ->row();

        $agent_admin_info['is_favorite'] = $is_exist ? 2 : 1;

        self::set_code(self::SUCCESS);
        self::set_msg("成功！");
        self::set_value("agent_admin_info",$agent_admin_info);
        self::set_value("agent_info",$agent_info);
        self::send();

    }

    /**
     * @功能：取得顾问评价
     * @开发者：gys
     */
    public function evaluate_list(){
        //接收参数
        $agent_admin_id  = self::get_data('agent_admin_id',self::NOT_EMPTY,"顾问编号不能为空！");
        $bottom_id = self::get_data('bottom_id');

        if(!empty($bottom_id)) $where['concat(1,lpad(agent_admin_evaluate_id,8,0))'] = array("lt",$bottom_id);
        $where['aae.agent_admin_id'] = $agent_admin_id;
        $evaluate_list = IDb::getInstance('agent_admin_evaluate aae')
            ->setDbFiled('aae.agent_admin_evaluate_id,aae.content,aae.create_time,aae.is_anonymous,u.avatar,u.real,u.sex')
            ->setDbJoin("user as u","u.user_id=aae.user_id ")
            ->setDbWhere($where)
            ->setDbOrder("agent_admin_evaluate_id desc")
            ->setDbLimit(10)
            ->sel();

        foreach ($evaluate_list as &$val){
            if ($val['is_anonymous'] == IsAnonymous::Yes){
                $val['real'] = get_real_cover($val['real'],$val['sex']);
                $val['avatar'] = '';
            }

            $val['create_time'] = date('Y.m.d H:i',strtotime($val['create_time']));

            //设置排序编码
            $val['code'] = '1'.str_pad($val['agent_admin_evaluate_id'],8,0,STR_PAD_LEFT);
        }

        //操作成功
        self::set_code(self::SUCCESS);
        self::set_msg("成功！");
        self::set_value("evaluate_list",$evaluate_list);
        self::send();
    }
}
